#coding:utf-8
'''
Created on 2012-7-26

@author: wb_suoyao.shen
'''
import datetime
from django.shortcuts import render_to_response
from django.views.decorators.csrf import csrf_exempt
from django.http import HttpResponse
from ssyblog.links.models import Links
from ssyblog.links.forms import LinkForm

def addLinks(request):
    nav_parent = 'System Settind'
    nav_child = 'Recomend Links'
    current_nav = 'recd_links'
    return render_to_response('cms/links/links_add.html',locals())

def queryLinks(request):
    links_list = Links.objects.all()
    nav_parent = 'System Settind'
    nav_child = 'Recomend Links'
    current_nav = 'recd_links'
    return render_to_response('cms/links/links_query.html',locals())

@csrf_exempt
def saveLinks(request):
    if request.method == 'POST':
        form = LinkForm(request.POST)
        if form.is_valid():
            obj = form.save(commit=False)
            obj.create_time = datetime.datetime.now()
            obj.save()
    else:
        form = LinkForm()
    tips = 'create links success'
    nav_parent = 'System Settind'
    nav_child = 'Recomend Links'
    current_nav = 'recd_links'
    return render_to_response('cms/links/links_add.html',locals())

def deleteLinks(request,linkId):
    if linkId:
        Links.objects.get(pk=linkId).delete()
    return queryLinks(request);

def deleteLinksBatch(request):
    if request.is_ajax():
        if request.method == 'GET':
            links_id = request.GET.get('linkId')
            if links_id:
                ids = links_id.split(',')
                try:
                    Links.objects.deleteLinksBatch(ids)
                except:
                    return HttpResponse('failed')
    return HttpResponse('success')